﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Com.CMS263.PO;
using Com.CMS263.BLL;
using Com.CMS263.Util;


namespace Com.CMS263.Web.manage
{
    public partial class manage_sendemail : System.Web.UI.Page
    {
        public int count = 0;
        SendEmailBLL bll = new SendEmailBLL();
        int pageSize = 50;
        static int pageCount = 0;
        public int CurrentIndex
        {
            get { return Convert.ToInt32(ViewState["index"]); }
            set { ViewState["index"] = value; }
        }
        public IList<SendEmail> list = new List<SendEmail>();
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                BindData();
            }
        }
        public void BindData()
        {
            CurrentIndex = Convert.ToInt32(Request.QueryString.Get("page"));
            if (CurrentIndex == 0)
            {
                CurrentIndex = 1;
            }
            string email = Request["email"];
            if (email == null)
            {
                email = "";
            }
            list = bll.GetListByPage(CurrentIndex, pageSize,email);
            pageCount = bll.GetCount(email);
           
        }

        
        public string GetPager()
        {

            return Pager.GetPager(pageCount, pageSize, CurrentIndex,"");
        }

        protected void btnSend_Click(object sender, EventArgs e)
        {
            EmailSetBLL sbll = new EmailSetBLL();
            string title = txtTitle.Text;
            string contents = Hidden1.Value;
            if (string.IsNullOrEmpty(title))
            {
                StringUtil.Alert("请输入发送标题", "-1");
            }
            else if (string.IsNullOrEmpty(contents))
            {
                StringUtil.Alert("请输入发送内容", "-1");
            }
            else
            {
                EmailSetBLL esbll = new EmailSetBLL();
                EmailSet entity = new EmailSet();
                try
                {
                    entity = sbll.GetSet()[0];
                    txtTitle.Text = "";
                    Hidden1.Value = "";
                    int type =Convert.ToInt32(hfSendType.Value);
                    if (type == 0)
                    {
                        IList<SendEmail> eslist = new List<SendEmail>();
                        eslist = bll.GetAll();
                        for (int i = 0; i < eslist.Count; i++)
                        {
                            try
                            {

                                StringUtil.SendSMTPEMail(entity.Server,entity.MailUser,entity.MailPass, eslist[i].UserEmail, title, contents);
                                count++;
                            }
                            catch
                            {
                                StringUtil.Alert("发送邮箱有问题,请核实", "-1");
                            }

                        }
                    }
                    else
                    {
                        string emaillist = Request["ckb"];
                        string[] emails = emaillist.Split(',');
                        for (int i = 0; i < emails.Length; i++)
                        {
                            if (emails[i] != null && emails[i] != "")
                            {
                                try
                                {

                                    StringUtil.SendSMTPEMail(entity.Server, entity.MailUser, entity.MailPass, emails[i], title, contents);
                                    count++;
                                }
                                catch
                                {
                                    StringUtil.Alert("发送邮箱有问题,请核实", "-1");
                                }
                            }
                        }
                    }
                    

                    StringUtil.Alert("发送完毕,成功发送" + count + "条", "-1");
                }
                catch (Exception)
                {
                    StringUtil.Alert("请先配置发送邮箱", "emailset.aspx");
                }
            }
        }
        
    }
}
